using System.Xml;
using NUnit.Framework;
using Sedge.Core;
using Sedge.Core.Configuration;
using Sedge.Core.FileSystemWrap;

namespace Sedge.Tests.Configuration
{
	[TestFixture]
	public class XmlToSuiteTest
	{
		private XmlDocument _xmlDocument;
		private XmlNode _rootNode;
		private XmlNode _suiteNode;

		[SetUp]
		public void Setup()
		{
		    Services.FileSystem = new FileSystem();

			_xmlDocument = new XmlDocument();
			_rootNode = _xmlDocument.CreateNode(XmlNodeType.Element, "Sedge", "");
			_xmlDocument.AppendChild(_rootNode);

			_suiteNode = _xmlDocument.CreateNode(XmlNodeType.Element, "Suite", "");
			XmlAttribute attr = _xmlDocument.CreateAttribute("name");
			attr.Value = "1";
			_suiteNode.Attributes.Append(attr);
			attr = _xmlDocument.CreateAttribute("caption");
			attr.Value = "2";
			_suiteNode.Attributes.Append(attr);
			attr = _xmlDocument.CreateAttribute("partial");
			attr.Value = "no";
			_suiteNode.Attributes.Append(attr);
			_rootNode.AppendChild(_suiteNode);
		}

		[Test]
		public void GetSuiteNodeTest()
		{
			XmlNode node = XmlToSuite.GetSuiteNode(_xmlDocument, false);
			Assert.IsTrue(node.Equals(_suiteNode));
		}
       
		[Test]
		public void CreateReportableObjectFromXmlTest()
		{
			XmlNode suiteNode = XmlToSuite.GetSuiteNode(_xmlDocument, false);
			Suite suite = XmlToSuite.CreateReportableObjectFromXml<Suite>(suiteNode, null, null);
			Assert.IsNotNull(suite);
			Assert.IsTrue(suite.Name.Equals("1"));
			Assert.IsTrue(suite.Caption.Equals("2"));
		}
        
		private void AddOptionNode(XmlNode node, string name, string value)
		{
			XmlNode optionNode = _xmlDocument.CreateNode(XmlNodeType.Element, "Option", "");
			XmlAttribute attr = _xmlDocument.CreateAttribute("name");
			attr.Value = name;
			optionNode.Attributes.Append(attr);
			attr = _xmlDocument.CreateAttribute("value");
			attr.Value = value;
			optionNode.Attributes.Append(attr);
			node.AppendChild(optionNode);
		}

		[Test]
		public void GetOptionsForNodeTest()
		{
			XmlNode node = _xmlDocument.CreateNode(XmlNodeType.Element, "CustomProperty", "");

			AddOptionNode(node, "1", "11");
			AddOptionNode(node, "2", "22");

			Options options = XmlToSuite.GetOptionsForNode(null, node);

			Assert.IsTrue(options.OptionsDictionary.Count == 2);
		}
	}
}